package com.baizhi.controller;


import com.baizhi.entity.Student;
import com.baizhi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("findAll")
    public String findAll(Integer pageNow,  // pageNow：当前页
                          Integer rows,     // rows：每列显示的记录数
                          String searchCol,
                          String searchValue,
                          Model model) {
        pageNow = pageNow==null?1:pageNow;
        rows = rows==null?4:rows;
        List<Student> students = studentService.findAll(pageNow,rows,searchCol,searchValue);
        // 总条数
        Integer counts = studentService.totalCounts(searchCol,searchValue);
        // 计算总页数
        int totalPage = counts%rows==0 ? counts/rows : counts/rows+1;
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("students", students); // 将students添加到request作用域
        model.addAttribute("searchValue", searchValue); // 方便值的回显
        model.addAttribute("searchCol", searchCol);
        return "back/student/index";
    }

}
